export default {
  namespaced: true,
  state: () => ({
    list: JSON.parse(localStorage.getItem("list") || "[]"),
    isSel: "all",
  }),
  mutations: {
    updateState(state, payload) {
      // console.log(payload.id);
      let index = state.list.findIndex((item) => item.id == payload.id);
      state.list[index].done = payload.value;
    },
    addState(state, payload) {
      state.list.push({
        id:
          state.list.length === 0
            ? 1
            : state.list[state.list.length - 1].id + 1,
        task: payload.value,
        done: false,
      });
    },
    delState(state, payload) {
      let index = state.list.findIndex((item) => item.id === payload.id);
      state.list.splice(index, 1);
    },
    changeTask(state, val) {
      state.isSel = val;
    },
    clearDone(state) {
      let arr = state.list.filter((item) => !item.done);
      state.list = arr;
    },

    changeState(state, payload) {
      state.isSel = payload;
    },
    updateAllState(state, val) {
      state.list.forEach((item) => (item.done = val));
    },
  },
  action: {},
  getters: {
    change(state) {
      if (state.isSel === "no") {
        return state.list.filter((item) => !item.done);
      } else if (state.isSel === "yes") {
        return state.list.filter((item) => item.done);
      } else {
        return state.list;
      }
    },
  },
};
